import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class WedDownloader {

	public static List<String> getLinksFromUrl(String inputURL) throws IOException {

		List<String> page = webReader(inputURL);
		List<String> links = new ArrayList<>(page.size());
		final Pattern linkRegex = Pattern.compile("(href|src)\\s*=\\s*\"(\\S+)\"");   //for href|src FINAL
		String link;
		Matcher matcherFirst;

		for ( String line : page ) {
			matcherFirst = linkRegex.matcher(line);
			while ( matcherFirst.find() ) {
				link = matcherFirst.group(2);
				if ( ! link.startsWith("http://") ) {
					if ( link.startsWith("//") ) {
						link = "http:" + link;
					} else if ( link.startsWith("/") ) {
						link = inputURL + link.replaceFirst("/", "");
					} else {
						link = inputURL + link;
					}
				}
				links.add(link);

			}
		}
		return links;
	}


	public static void saveLocal(String inputUrl, String path) throws IOException, IllegalAccessException {


		if ( inputUrl.contains("htm") ) {
			saveLocalHtmType(inputUrl, path);
		}


		File folderToSave = new File(path);
		File fileToSave;
		String fileName;
		String[] splitted = inputUrl.split("/");
		fileName = splitted[splitted.length - 1];

		if ( fileName.matches(".*[\\*\\\\/\\:\\?\"\\<\\>\\|].*") ) {
			return;
		}
		if ( ! fileName.contains(".") ) {
			fileName += ".htm";
			saveLocalHtmType(inputUrl, path);
		}
		//creating folder
		if ( ! folderToSave.exists() ) {
			if ( ! folderToSave.mkdirs() ) {
				throw new IOException("trouble with creating " + folderToSave);
			}
		}
		System.err.println("BEFORE SAVING" + fileName);
		fileToSave = new File(folderToSave, fileName);
		if ( ! fileToSave.exists() ) {
			if ( ! fileToSave.createNewFile() ) {
				throw new IOException("trouble with creating " + fileToSave);
			}
		}


//		Writer writer = new FileWriter(result);
	}

	public static void saveLocalHtmType(String inputUrl, String path) throws IOException {

		List<String> fileStringList = webReader(inputUrl); //read web page
		List<String> linksList = getLinksFromUrl(inputUrl); //read links

	}

	public static List<String> webReader(String inputURL) throws IOException {
		URL currentUrl = new URL(inputURL);
		String line = null;
		List<String> result = new ArrayList<>();

		URLConnection connection = currentUrl.openConnection();
		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
		while ( (line = bufferedReader.readLine()) != null ) {
			result.add(line);
		}
		return result;


	}

	public static String checkContent(String inputURL) throws IOException {
		System.err.println("Trying link " + inputURL);
		URL currentURL = new URL(inputURL);
		URLConnection connection = currentURL.openConnection();
		return connection.getContentType();
	}


}
